

package com.yungu.swift.assets.driver.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.assets.driver.dao.DriverStatusCycleMapper;
import com.yungu.swift.assets.driver.model.dto.DriverStatusCycleDto;
import com.yungu.swift.assets.driver.service.DriverStatusCycleService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 司机里程统计服务类实现
 * @Author cuixiuyin
 * @Date 2018/10/26 14:20
 * @Version 1.0
 */
@Service
public class DriverStatusCycleServiceImpl extends BaseServiceImpl<DriverStatusCycleDto> implements DriverStatusCycleService {

    @Autowired
    private DriverStatusCycleMapper driverStatusCycleMapper;

    @Override
    protected IMapper<DriverStatusCycleDto> getMapper() {
        return driverStatusCycleMapper;
    }


    @Override
    public ResponseData<Integer> getTodayMaxSort(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(driverStatusCycleMapper.getTodayMaxSort(params));
    }

    @Override
    public ResponseData<DriverStatusCycleDto> queryOne(Map<String, Object> params) {
        DriverStatusCycleDto driverStatusCycleDto = null;
        ResponseData<List<DriverStatusCycleDto>> responseData = this.queryByActive(params);
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            driverStatusCycleDto = responseData.getData().get(0);
            params.put("uuid", driverStatusCycleDto.getUuid());
            driverStatusCycleMapper.removeErrorActive(params);
        }
        return ResponseData.buildSuccessResponse(driverStatusCycleDto);
    }


    @Override
    public ResponseData<List<DriverStatusCycleDto>> queryByActive(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(driverStatusCycleMapper.queryByActive(params));
    }

    @Override
    public ResponseData<DriverStatusCycleDto> queryById(String uuid) {
        return ResponseData.buildSuccessResponse(driverStatusCycleMapper.queryById(uuid));
    }

    @Override
    public ResponseData<Integer> queryWorkTimeSum(String driverUuid, String recordDate) {
        Integer workTime = 0;
        if (StringUtils.isNotBlank(driverUuid)) {
            if (StringUtils.isBlank(recordDate)) {
                recordDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            }
            Map<String, Object> params = MapUtils.build(3);
            params.put("driverUuid", driverUuid);
            params.put("recordDate", recordDate);
            List<DriverStatusCycleDto> driverStatusCycleDtos = driverStatusCycleMapper.queryOnWorkStatusCycleByDate(params);
            if (CollectionUtils.isNotEmpty(driverStatusCycleDtos)) {
                for (DriverStatusCycleDto driverStatusCycleDto : driverStatusCycleDtos) {
                    if (driverStatusCycleDto.getActive().equals(DriverStatusCycleDto.ACTIVE_ON)) {
                        workTime += driverStatusCycleDto.getStatusTime();
                    } else if (driverStatusCycleDto.getActive().equals(DriverStatusCycleDto.ACTIVE_OFF)) {
                        workTime += new Long((System.currentTimeMillis() - driverStatusCycleDto.getStartTime().getTime()) / 1000).intValue();
                    }
                }
            }
        }
        return ResponseData.buildSuccessResponse(workTime);
    }
}
